package com.cn.why.film.filmindex.service.serviceImpl;

import com.cn.why.film.filmindex.aop.MyRedisCache;
import com.cn.why.film.filmindex.entity.IndexTitle;
import com.cn.why.film.filmindex.entity.PetProduce;
import com.cn.why.film.filmindex.entity.ProOutlineInfomation;
import com.cn.why.film.filmindex.repository.InformationRepository;
import com.cn.why.film.filmindex.repository.PetIndexRepository;
import com.cn.why.film.filmindex.repository.PetMartReposity;
import com.cn.why.film.filmindex.service.PetIndexService;
import com.cn.why.film.filmindex.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 实现类
 */
@Service
public class PetIndexServiceImpl implements PetIndexService {
    @Autowired
    private PetIndexRepository cenDao;
    @Autowired
    private InformationRepository infomationDao;
    @Autowired
    private PetMartReposity martDao;

    /**业务处理层*/
    //取到需要展示前四个的首页信
    @Override
    @MyRedisCache(key = "IndexFourList",expired = 3000)//加入缓存中判断是否存在，设置过期时间为3000s
    public List<IndexTitle> getIndexFourList(Integer id) {
        List<IndexTitle> listFour = new ArrayList<IndexTitle>(4);
        List<IndexTitle> listAll = new ArrayList<>();
        listAll = findAll();
        //获取size的大小
        int size = findAll().size();
        if(size<4){
            listFour = listAll;
        }else {
            //加入到listfour中
            for (int i =size-4;i<size;i++){
                listFour.add(listAll.get(i));
            }
            System.out.println("执行查询数据库");
            //判断应该展示的内容
            if (id!=null){
                //移除最后一个
                listFour.remove(3);
                //添加最新的一个
                listFour.add(0,findOneById(id));
            }
        }
       return listFour;
    }

    //查询所有
    @Override
    public List<IndexTitle> findAll() {
        return cenDao.findAll();
    }

    //模糊查询
    @Override
    public List<IndexTitle> findByPetName(String text) {
        return cenDao.findByPetName(text);
    }
    //查询单独
    @Override
    public IndexTitle findOneById(Integer id) {
        return cenDao.findOne(id);
    }

    //插入操作
    @Override
    @MyRedisCache(key = "IndexFourList",select = false)
    public void insertIndexTitle(IndexTitle indexTitle) {
        //添加
        cenDao.save(indexTitle);
    }

    /************************************主页右侧信息展示相关*********************************/
    //信息分页查询（默认展示八条）
    @Override
    @MyRedisCache(key = "findNoCriteria",expired = 300)
    public List<ProOutlineInfomation> findNoCriteria(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page,size, Sort.Direction.DESC,"id");
        return infomationDao.findAll(pageable).getContent();
    }

    /**
     *折扣商品
     * @return
     */
    @Override
    @MyRedisCache(key = "getDisCountPro",expired = 300)
    public List<PetProduce> getDisCountPro() {
        return martDao.getDisCountPro();
    }

    /**
     * 获得商品的类型编号
     * @param proType
     * @return
     */
    @Override
    public Integer getProNumber(String proType) {
        return infomationDao.getProNumber(proType);
    }

    /**
     * 从商品表中取得数据
     */
    public List<PetProduce> getProduceByNumber(Integer proNumber){
        return martDao.getProduceByNumber(proNumber);
    }

    @Override
    public List<PetProduce> getProduceByName(String proName) {
        return martDao.getProduceByName(proName);
    }
}
